<?php declare(strict_types=1);
/**
 * @author      guosir
 * @copyright   2023-2025 owner
 **/

namespace Srv\Apps\Apis\Task;

use Srv\Apps\Apis\Log\Ctl\LogCtl;
use Srv\Apps\Apis\Task\Ctl\TaskTplCtl;
use Srv\Apps\Proto\DTO\Task\TaskTplAddDTO;
use Srv\Apps\Proto\DTO\Task\TaskTplEditDTO;
use Srv\Apps\Proto\DTO\Task\TaskTplListDTO;
use Srv\Apps\Proto\Enum\ErrCodeEnum;
use Srv\Apps\Proto\Enum\LangKeyEnum;
use Srv\Apps\Proto\Enum\Log\LogResultEnum;
use Srv\Apps\Proto\VO\Task\TaskTplListVO;
use Srv\Libs\ConfModel\RouteConfData;
use Srv\Libs\Frame\ActionAbstract;
use Srv\Libs\Frame\DTOAbstract;
use Srv\Libs\Frame\Lang;
use Srv\Libs\Frame\Page;
use Srv\Libs\Frame\ResponseData;
use Srv\Libs\Frame\VOAbstract;

final class TaskTplAct extends ActionAbstract
{
    /**
     * @url /task/tpl/list
     * @auth getTaskTplList
     * @req Srv.Apps.Proto.DTO.Task.TaskTplListDTO
     * @rsp Srv.Apps.Proto.VO.Task.TaskTplListVO
     * 任务模板列表
     */
    public function getTaskTplList(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass):bool
    {
        if(!($DTOClass instanceof TaskTplListDTO) || !($VOClass instanceof TaskTplListVO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $Page                   = new Page();
        $currPage               = max(1, $DTOClass->getCurrPage());
        $pageSize               = $DTOClass->getPageSize() < 1 ? 12 : $DTOClass->getPageSize();
        $Page->init(0, $pageSize, $currPage);
        $whereData              = $RouteConfData->getParamsResData($DTOClass);
        $VOClass                = TaskTplCtl::tag()->getTaskTplList($Page, $whereData);
        return ResponseData::rspSuccAuto('', $VOClass, []);
    }

    /**
     * @url /task/tpl/add
     * @auth taskTplAdd
     * @req Srv.Apps.Proto.DTO.Task.TaskTplAddDTO
     * 任务模板新增
     */
    public function taskTplAdd(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass):bool
    {
        unset($VOClass);
        if(!($DTOClass instanceof TaskTplAddDTO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $setData                = $RouteConfData->getParamsResData($DTOClass);
        $logData                = $setData;
        $LogCtl                 = LogCtl::tag();
        $result                 = TaskTplCtl::tag()->taskTplAdd($setData);
        if(!$result){
            $errMsg             = '任务模板新增失败';
            $LogCtl->logAdd(LogCtl::TASK_TPL_ADD, LogResultEnum::LOG_RESULT_FAIL, $errMsg, $logData);
            return ResponseData::rspErrAuto(ErrCodeEnum::ERR_FAIL, Lang::getString(LangKeyEnum::_LANG_FAILED));
        }
        $errMsg                 = '任务模板新增成功';
        $LogCtl->logAdd(LogCtl::TASK_TPL_ADD, LogResultEnum::LOG_RESULT_SUCCESS, $errMsg, $logData);
        return ResponseData::rspSuccAuto('', null, []);
    }

    /**
     * @url /task/tpl/edit
     * @auth taskTplEdit
     * @req Srv.Apps.Proto.DTO.Task.TaskTplEditDTO
     * 任务模板编辑
     */
    public function taskTplEdit(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass):bool
    {
        unset($VOClass);
        if(!($DTOClass instanceof TaskTplEditDTO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $taskId                 = intval($DTOClass->getTptkId());
        $setData                = $RouteConfData->getParamsResData($DTOClass);
        $TaskTplCtl             = TaskTplCtl::tag();
        $LogCtl                 = LogCtl::tag();
        $infoSrc                = $TaskTplCtl->getTaskTplInfoSrc($taskId);
        if(!isset($infoSrc['tptk_id'])){
            $logData            = $setData;
            $errMsg             = '任务模板不存在';
            $LogCtl->logAdd(LogCtl::TASK_TPL_EDIT, LogResultEnum::LOG_RESULT_FAIL, $errMsg, $logData);
            return ResponseData::rspErrAuto(ErrCodeEnum::ERR_FAIL, Lang::getString(LangKeyEnum::_LANG_FAILED));
        }
        $logData                = ['new' => $setData, 'old' => $infoSrc];
        $result                 = $TaskTplCtl->taskTplEdit($taskId, $setData);
        if(!$result){
            $errMsg             = '任务模板编辑失败';
            $LogCtl->logAdd(LogCtl::TASK_TPL_EDIT, LogResultEnum::LOG_RESULT_FAIL, $errMsg, $logData);
            return ResponseData::rspErrAuto(ErrCodeEnum::ERR_FAIL, Lang::getString(LangKeyEnum::_LANG_FAILED));
        }
        $errMsg                 = '任务模板编辑成功';
        $LogCtl->logAdd(LogCtl::TASK_TPL_EDIT, LogResultEnum::LOG_RESULT_SUCCESS, $errMsg, $logData);
        return ResponseData::rspSuccAuto('', null, []);
    }
}